package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

//    redis内置了StringRedisTemplate 直接注入即可
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SystemUserMapper systemUserMapper;


    @Override
    public CaptchaVo getCaptcha() {
//        宽 高 位数
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        // 设置类型，纯数字、纯字母、字母数字混合
        specCaptcha.setCharType(Captcha.TYPE_DEFAULT);
//        specCaptcha.text()  获取验证码的字符
//        specCaptcha.textChar() 获取验证码的字符数组

        String code = specCaptcha.text().toLowerCase();
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID();
        String image = specCaptcha.toBase64();
//        存入到集群的redis中 ttl=一分钟
        stringRedisTemplate.opsForValue().set(key, code, RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);

        return new CaptchaVo(image, key);
    }


//    校验流程  有无验证码 -> 是否正确 -> 用户名密码是否存在且正确
    @Override
    public String login(LoginVo loginVo) {
        //1.判断验证码是否为null
        if(!StringUtils.hasLength(loginVo.getCaptchaCode())){

            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }

        //2.校验验证码的key是否有效
        if(!stringRedisTemplate.hasKey(loginVo.getCaptchaKey())){

            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }

        //3.校验验证码是否正确
        String code = stringRedisTemplate.opsForValue().get(loginVo.getCaptchaKey());
        if(!code.equals(loginVo.getCaptchaCode())){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }

        //4.根据用户名和密码查询数据库
        LambdaQueryWrapper<SystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemUser::getUsername,loginVo.getUsername())
                .eq(SystemUser::getPassword, DigestUtils.sha256Hex(loginVo.getPassword()));
        SystemUser systemUser = systemUserMapper.selectOne(lambdaQueryWrapper);

        if(systemUser == null){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }

        //如果代码执行到这里说明用户名和密码正确  返回产生的token
        return JwtUtil.createToken(systemUser.getId());
    }
}
